package nc.bs.arap.Bo;

import java.util.ArrayList;
import java.util.List;

import nc.appcan.bill.itf.ITbbService;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.busibean.ISysInitQry;
import nc.jdbc.framework.processor.ArrayListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.ui.pub.ClientEnvironment;
import nc.vo.ep.dj.DJZBHeaderVO;
import nc.vo.ep.dj.DJZBItemVO;
import nc.vo.ep.dj.DJZBVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.vv.tbb.TBBJSVO;

public class TbbZBBO {
	
	public TbbZBBO() {
		super();
	}

	private boolean isShowPkCorp(String pk_corp){
		boolean  is= false;
		try {
			ISysInitQry iSysInitQry = NCLocator.getInstance().lookup(
					ISysInitQry.class);
			is = iSysInitQry.getParaBoolean(pk_corp, "CMP56").booleanValue();
		} catch (Exception e) {
		}
		return is;
	}
	
	public void checkandsave(DJZBVO djzbvo) throws BusinessException, nc.vo.pub.BusinessException {
        if(!checkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm()))return;
		 DJZBItemVO[]  items  = ( DJZBItemVO[] )djzbvo.getChildrenVO();
		 List<TBBJSVO> list = new ArrayList<TBBJSVO>();
		 boolean show =isShowPkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm());
		 for(DJZBItemVO item:items){
			 TBBJSVO  vo = new TBBJSVO();
			 vo.setPk_corp(item.getCorpPk());
			 vo.setPk_dept(show?item.getDeptid():"~");
			 vo.setPk_szxm(item.getSzxmid());
			 vo.setBilldate(((DJZBHeaderVO)djzbvo.getParentVO()).getDjrq());
			 vo.setMoney(item.getJfbbje());
			 vo.setPk_bill(((DJZBHeaderVO)djzbvo.getParentVO()).getVouchid());
			 vo.setPk_billtype(((DJZBHeaderVO)djzbvo.getParentVO()).getDjlxbm());
			 vo.setPk_bill_b(item.getFb_oid());
			 list.add(vo);
		 }
		 ITbbService tbb = NCLocator.getInstance().lookup(ITbbService.class);
		 tbb.executeTbb(new UFBoolean(false), list.toArray(new TBBJSVO[list.size()] ));
	}
	public void delete(DJZBVO djzbvo) throws BusinessException, nc.vo.pub.BusinessException {
		  if(!checkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm()))return;
		 DJZBItemVO[]  items  = ( DJZBItemVO[] )djzbvo.getChildrenVO();
		 List<TBBJSVO> list = new ArrayList<TBBJSVO>();
		 boolean show =isShowPkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm());
		 for(DJZBItemVO item:items){
			 TBBJSVO  vo = new TBBJSVO();
			 vo.setPk_corp(item.getCorpPk());
			 vo.setPk_dept(show?item.getDeptid():"~");
			 vo.setPk_szxm(item.getSzxmid());
			 vo.setBilldate(((DJZBHeaderVO)djzbvo.getParentVO()).getDjrq());
			 vo.setMoney(item.getJfbbje().multiply(-1));
			 vo.setPk_bill(((DJZBHeaderVO)djzbvo.getParentVO()).getVouchid());
			 vo.setPk_billtype(((DJZBHeaderVO)djzbvo.getParentVO()).getDjlxbm());
			 vo.setPk_bill_b(item.getFb_oid());
			 list.add(vo);
		 }
		 ITbbService tbb = NCLocator.getInstance().lookup(ITbbService.class);
		 tbb.executeTbb(new UFBoolean(true), list.toArray(new TBBJSVO[list.size()] ));
	}
	public void edit(DJZBVO djzbvo) throws BusinessException, nc.vo.pub.BusinessException {
		 if(!checkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm()))return;
		 DJZBItemVO[]  items  = ( DJZBItemVO[] )djzbvo.getChildrenVO();
		 List<TBBJSVO> list = new ArrayList<TBBJSVO>();
		 boolean show =isShowPkCorp(((DJZBHeaderVO)djzbvo.getParentVO()).getDwbm());
		 for(DJZBItemVO item:items){
			 TBBJSVO  vo = new TBBJSVO();
			 vo.setPk_corp(item.getCorpPk());
			 vo.setPk_dept(show?item.getDeptid():"~");
			 vo.setPk_szxm(item.getSzxmid());
			 vo.setBilldate(((DJZBHeaderVO)djzbvo.getParentVO()).getDjrq());
			 vo.setMoney(item.getJfbbje());
			 vo.setVdef1(item.m_oldjfbbje);
			 vo.setPk_bill(((DJZBHeaderVO)djzbvo.getParentVO()).getVouchid());
			 vo.setPk_billtype(((DJZBHeaderVO)djzbvo.getParentVO()).getDjlxbm());
			 vo.setPk_bill_b(item.getFb_oid());
			 list.add(vo);
		 }
		 ITbbService tbb = NCLocator.getInstance().lookup(ITbbService.class);
		 tbb.executeTbb(new UFBoolean(false), list.toArray(new TBBJSVO[list.size()] ));
	}
    
	private boolean checkCorp(String pk_corp){
		BaseDAO dao = new BaseDAO();
		try {
			List<String>  sqls =(List<String>) dao.executeQuery("select pk_corp from appcan_vvtb_corp",new  ColumnListProcessor());
			if(sqls!=null&&sqls.size()>0){
				if(sqls.contains(pk_corp))return true;
				else return false;	
			}
		} catch (DAOException e) {
			e.printStackTrace();
		}
		 return true;
	}
	
	
}